package com.yjz.common.util

import android.os.Handler
import android.os.Looper
import android.os.Message

/**
 *作用 :
 *作者 : yjz
 *时间 : 2025/3/21 17:01
 */
object MainHandler {

    private val handler: Handler = Handler(Looper.getMainLooper())

    fun post(runnable: Runnable) {
        handler.post(runnable)
    }

    fun postDelay(delayMills: Long, runnable: Runnable) {
        handler.postDelayed(runnable, delayMills)
    }

    // 优先处理任务
    fun sendAtFrontOfQueue(runnable: Runnable) {
        val msg = Message.obtain(handler, runnable)
        //把消息插到最前面，使其最快执行
        handler.sendMessageAtFrontOfQueue(msg)
    }

    fun remove(runnable: Runnable) {
        handler.removeCallbacks(runnable)
    }
}